import helpTaskApi from '@/api/task/helpTask'
import taskStepApi from '@/api/task/taskStep'
import proofApi from '@/api/task/proof'
import permission from '@/directive/permission/index.js'

export default {
  //如果需要标签页缓存生效，则需要保证name值和菜单管理中的编码值一致
  name: 'helpTask',
  directives: { permission },
  data() {
    return {
      formVisible: false,
      innerVisible: false,
      formTitle: '添加帮助任务',
      isAdd: true,
      isAudit: false,
      form: {
        classifyId:'',
        title:'',
        num:'',
        lastNum:'',
        state:'',
        sketch:'',
        timeLimit:'',
        endTime:'',
        cityId:'',
        id: ''
      },
      failureReason:'',
      options: [
        { label: '待支付', value: 0 },
        { label: '进行中', value: 1 },
        { label: '审核中', value: 2 },
        { label: '待修改', value: 3 },
        { label: '已结束', value: 5 },
        { label: '暂停中', value: 6 }
      ],
      citys: [],
      taskSteps: [],
      taskPoofs: [],
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
        title: undefined,
        classifyId: undefined,
        state: undefined,
      },
      classifys: [],
      total: 0,
      list: null,
      listLoading: true,
      selRow: {}
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  computed: {

    //表单验证
    rules() {
      return {
        // cfgName: [
        //   { required: true, message: this.$t('config.name') + this.$t('common.isRequired'), trigger: 'blur' },
        //   { min: 3, max: 2000, message: this.$t('config.name') + this.$t('config.lengthValidation'), trigger: 'blur' }
        // ]
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
        helpTaskApi.getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
      helpTaskApi.getCity().then(response =>{
        this.citys = response.data
      })
      helpTaskApi.getClassify().then(response =>{
        this.classifys = response.data
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.listQuery.classifyId = ''
      this.listQuery.title = ''
      this.listQuery.state = ''
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    resetForm() {
      this.form = {
        classifyId:'',
        title:'',
        num:'',
        lastNum:'',
        state:'',
        sketch:'',
        timeLimit:'',
        endTime:'',
        cityId:'',
        id: ''
      }
    },
    add() {
      this.formTitle = '添加帮助任务'
      this.formVisible = true
      this.isAdd = true

      if(this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      //如果表单初始化有特殊处理需求,可以在resetForm中处理
          },
    audit(record){
      this.formTitle = '审核帮助任务'
      this.formVisible = true
      this.isAudit = true
      this.getStepAndProof(record.id)
      this.selRow = record

    },
    view(record){
      this.formTitle = '查看帮助任务'
      this.formVisible = true
      this.isAudit = false
      this.getStepAndProof(record.id)
      this.selRow = record
    },getStepAndProof(taskId){
      taskStepApi.getStepByTaskId(taskId).then(response =>{
        this.taskSteps = response.data
        for(let i = 0;i<this.taskSteps.length;i++){
          if(this.taskSteps[i].idFiles != null && this.taskSteps[i].idFiles != '' ){
            this.taskSteps[i].idFiles = this.taskSteps[i].idFiles.split(',')
          }else {
            this.taskSteps[i].idFiles = []
          }

        }
      })
      proofApi.getProofByTaskId(taskId).then(response =>{
        this.taskPoofs = response.data
      })
    },
    pass(id){
      helpTaskApi.pass(id).then(response =>{
        if(response.code == 20000){
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.fetchData()
          this.formVisible = false
        }else {
          this.$message({
            message: '操作失败',
            type: 'warning'
          })
        }
      })},
    noPass(id){
      const formData = {
        taskId:id,
        failureReason:this.failureReason
      }
      helpTaskApi.noPass(formData).then(response =>{
        if(response.code==20000){
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.fetchData()
          this.formVisible = false
          this.innerVisible = false
        }else {
          this.$message({
            message: '操作失败',
            type: 'warning'
          })
        }
      })}
    ,
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
            const formData = {
                id:this.form.id,
                classifyId:this.form.classifyId,
                title:this.form.title,
                num:this.form.num,
                lastNum:this.form.lastNum,
                state:this.form.state,
                sketch:this.form.sketch,
                timeLimit:this.form.timeLimit,
                endTime:this.form.endTime,
                cityId:this.form.cityId,
            }
            if(formData.id){
                helpTaskApi.update(formData).then(response => {
                    this.$message({
                        message: this.$t('common.optionSuccess'),
                        type: 'success'
                    })
                    this.fetchData()
                    this.formVisible = false
                })
            }else{
                helpTaskApi.add(formData).then(response => {
                    this.$message({
                        message: this.$t('common.optionSuccess'),
                        type: 'success'
                    })
                    this.fetchData()
                    this.formVisible = false
                })
            }
        } else {
          return false
        }
      })
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    editItem(record){
      this.selRow = record
      this.edit()
    },
    edit() {
      if (this.checkSel()) {
        this.isAdd = false
        let form = Object.assign({}, this.selRow)
        this.form = form
        this.formTitle = '编辑帮助任务'
        this.formVisible = true

        if(this.$refs['form'] !== undefined) {
          this.$refs['form'].resetFields()
        }
        //如果表单初始化有特殊处理需求,可以在resetForm中处理
              }
    },
    removeItem(record){
      this.selRow = record
      this.remove()
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
            helpTaskApi.remove(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          }).catch( err=> {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    }

  }
}
